<template>
    <div class="headNav">
        <img class="logo" src="../assets/logo.png" alt="">
         <span class="title">羽毛球馆信息化管理系统</span>
         <div class="user">
             <img class="avatar" :src="userInfo.profile.avatarUrl?userInfo.profile.avatarUrl:defultAvatar" alt="">
             <span class ="username">{{userInfo.profile.username}}</span>
             <div class="icon" style="transition:0.3s" ><i class="el-icon-arrow-down"></i></div>
            <div class="downBox">
                <div class="buttonBox">
                    <div class="buttonItem"><button @click="showEditPassDialog">修改密码</button></div>
                    <div  class="buttonItem"><button id="quit" @click="quitLogin">退出登录</button></div>
                
                </div>
             
            </div>
         </div>
          <!-- <el-button class="quitBtn" type="danger" @click="quitLogin">退出</el-button> -->
          <!-- 修改密码对话框 -->
             <el-dialog width="600px" title="修改密码" :visible.sync="isShowEditPassword">
             <el-form ref="editPasswordFormRef"  label-width="120px" :model="editPasswordForm" :rules="rules">
                <el-form-item label="旧密码" prop="oldPassword">
                    <el-input v-model="editPasswordForm.oldPassword" type="password"  show-password clearable></el-input>
                </el-form-item>
                 <el-form-item label="新密码" prop="newPassword">
                    <el-input v-model="editPasswordForm.newPassword" type="password" clearable></el-input>
                </el-form-item>
                 <el-form-item label="再次输入密码" prop="repeat">
                    <el-input v-model="editPasswordForm.repeat" type="password" clearable></el-input>
                </el-form-item>
                 <el-form-item class="formBtnBox"> 
                    <el-button type="primary" @click="submitEditPassword">确定修改</el-button>
                    <el-button  @click="closeDialog">取消</el-button>
                </el-form-item>
                
             </el-form>
        </el-dialog>
    </div>
</template>

<script>
import Api from '../api'
import {mapState} from 'vuex'
export default {
    data() {
        var confirmPassword = (rule,value,callback) =>{
          
               if(value !== this.editPasswordForm.newPassword){
                    callback(new Error('两次输入密码不一致!'));
                }else {
                    callback();
                }
         }
        return{
            defultAvatar:'https://img1.baidu.com/it/u=4278052697,4017305663&fm=26&fmt=auto',
            isShowEditPassword:false,
            // 修改密码的表单
            editPasswordForm:{
                oldPassword:'',
                newPassword:'',
                repeat:'',
                uid:"",
                role:""
            },
            // 表单验证规则
            rules:{
                oldPassword:[{ required: true, message: '请输入旧密码', trigger: 'blur' },
                          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}],
                newPassword:[{ required: true, message: '请输入新密码', trigger: 'blur' },
                          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}],
                 
                repeat:[{validator:confirmPassword,required:true,trigger:'blur'}],
            },
        }
    },
    computed:{
         ...mapState('login',["userInfo"]),

    },
    methods: {
        quitLogin(){
            localStorage.removeItem('token');
            sessionStorage.removeItem('profile');
            sessionStorage.removeItem('menu');
            this.$router.push('/login');
        },
        // 确认修改密码后的回调
        async submitEditPassword(){
             // 验证表单
             this.$refs.editPasswordFormRef.validate(async valid=>{
                 if(!valid) return;
                 const {data:res} = await Api.editPassword(this.editPasswordForm);
                 if(res.status !== 200 ) return this.$message.error(res.msg);
                 this.$message.success('密码已修改');
                 this.closeDialog();
             })
        },
        // 显示修改密码对话框
        showEditPassDialog(){
            this.isShowEditPassword = true;

            this.editPasswordForm.uid = this.userInfo.profile.uid||this.userInfo.profile.id;
            this.editPasswordForm.role = this.userInfo.role;
        },
        closeDialog(){
            this.isShowEditPassword = false;
            this.$refs.editPasswordFormRef.resetFields()
        }
    
    },
}
</script>

<style scoped lang="less">
    .headNav{
        // background: #89cd6b;
        //  background-image: linear-gradient(to right, #30cfd0 0%, #330867 100%);
 background-image: linear-gradient(125deg,#67e4ef,#3ec3e7,#177cb0,#4b5cc4,#003371,#549688,#46b9cb,#2d97cc);
        position: relative;
        width: 100%;
        height: 80px;
        display: flex;
        align-items: center;
        background-size: 500%;
        animation: movingBG 15s infinite;
    }
    @keyframes movingBG{
        0%{
            background-position: 0% 50%;
        }
        50%{
            background-position: 100% 50%;
        }
        100%{
            background-position: 0% 50%;
        }
    }
    .logo{
        width: 55px;
        height: 55px;
    
    }
    .user{
        width: 240px;
        height: 60px;
        position: relative;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        margin-left: 60%;
        .username{
            width: 100px;
            color: #ddd;
        }
        
        .icon{
            color: rgb(179, 225, 251);
       
        }
        
    }
    .user:hover{
        background: #95bdc8ba;
        transition: 0.3s linear;
        cursor: pointer;
        border-radius: 10px 10px 0 0;
        .downBox{
            height: 100px;
            .buttonItem{
                visibility: visible;
            }
        }
        .icon{
            color: rgb(241, 250, 255);
            transform: rotate(180deg);
        }
    }
    .downBox{
        width: 240px;
        height: 0px;
        position: absolute;
        top: 60px;
        background: #95bdc8ba;
        z-index: 1;
        border-radius: 0 0 10px 10px;
        transition: 0.1s linear;
        .buttonBox{
            visibility: hidden;
            display: flex;
            flex-direction: column;
            align-content: space-between;
            height: auto;
           .buttonItem{
                visibility:hidden;  
                width: 240px;
                height: 40px;
                display: flex;
                justify-content: center;
                align-content: center;
                margin-bottom: 5px;
                button{
                    width: 200px;
                    background: #ebf1f5e8;
                    border: none;
                    border-radius: 6px;
                    font-size: 1em;
                    font-weight: 600;
                    cursor: pointer;
                    color: #630a54;

                }
                #quit {
                    background: rgb(224, 83, 83);
                }
            }
            .buttonItem:hover{
                button{
                    background: rgb(184, 181, 181);
                    color: white;
                }
                #quit{
                    background: rgb(177, 61, 61);
                    color: white;
                }
            }
        }
        
    }
    .avatar{
        width: 55px;
        height: 55px;
        border: 1px solid rgb(147, 231, 250);
        border-radius: 50%;
    }
    .quitBtn{
        margin-left: auto;
        margin-right: 20px;
    }
    .title{
        font-size: 21px;
        font-weight: 700;
        width: 260px;
        color: rgb(243, 243, 243);
    }
   
</style>